package com.cg.javafxdemospring;

import com.cg.javafxdemospring.service.ImportExcelService;
import com.cg.javafxdemospring.util.ExcelUtil;
import com.cg.javafxdemospring.util.SpringBeanAwareUtil;
import com.cg.javafxdemospring.util.UserStringUtil;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author 陈刚
 * @ClassName DemoApplication
 * @Description //TODO
 * @create 2020-08-06 09:28
 */
@SpringBootApplication
public class DemoApplication extends AbstractJavaFxApplicationSupport {
    private static void configureFileChooser(
            final FileChooser fileChooser) {
        fileChooser.setTitle("选择同步的文件");
        fileChooser.setInitialDirectory(
                new File(System.getProperty("user.home"))
        );
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("XLS", "*.xls"),
                new FileChooser.ExtensionFilter("XLSX", "*.xlsx")
        );
    }

    public void loadMenuBar(GridPane gridPane) {
        MenuBar menuBar = new MenuBar();

        // --- Menu File
        Menu menuFile = new Menu("File");

        // --- Menu Edit
        Menu menuEdit = new Menu("Edit");

        // --- Menu View
        Menu menuView = new Menu("View");

        menuBar.getMenus().addAll(menuFile, menuEdit, menuView);
        gridPane.getChildren().add(menuBar);
        GridPane.setConstraints(menuBar, 0, 1);
    }

    @Override
    public void start(final Stage stage) {
        stage.setTitle("sampleresult同步工具");
        stage.getIcons().add(new Image(
                DemoApplication.class.getResourceAsStream("/icons/sync.png")));

        final FileChooser fileChooser = new FileChooser();
        final Button openButton = new Button("打开需同步的文件");
        ImageView imageView = new ImageView("/excel.png");
        imageView.setFitWidth(20);
        imageView.setFitHeight(20);
        openButton.setGraphic(imageView);
        openButton.setPrefWidth(200);
        openButton.setPrefHeight(20);
        final TextArea textArea = new TextArea("同步明细");
        textArea.setText("同步明细：\n");
        textArea.setPrefHeight(400);
        textArea.setPrefWidth(600);
        textArea.setEditable(false);

        openButton.setOnAction(
                (final ActionEvent e) -> {
                    configureFileChooser(fileChooser);
                    File file = fileChooser.showOpenDialog(stage);
                    if (file != null) {
//                        openFile(file);
                        ExecutorService executorService = Executors.newSingleThreadExecutor();
                        executorService.execute(()->syncData(file,textArea));

                    }
                });


        final GridPane inputGridPane = new GridPane();

        GridPane.setConstraints(openButton, 0, 0);
        GridPane.setConstraints(textArea, 0, 1);
        inputGridPane.setHgap(6);
        inputGridPane.setVgap(6);
        inputGridPane.getChildren().addAll(textArea,openButton);

//        loadMenuBar(inputGridPane);

        final Pane rootGroup = new VBox(12);
        rootGroup.getChildren().addAll(inputGridPane);
        rootGroup.setPadding(new Insets(12, 12, 12, 12));

        stage.setScene(new Scene(rootGroup));
        stage.show();
    }

    @Autowired
    ImportExcelService importExcelService;

    @SneakyThrows
    private void syncData(File file, TextArea textArea) {
        List<List<String>> lists = ExcelUtil.readExcel(file, UserStringUtil.getSuffix(file.getName()));
        lists.remove(0);
        ApplicationContext applicationContext = SpringBeanAwareUtil.getApplicationContext();
        importExcelService = applicationContext.getBean(ImportExcelService.class);
        importExcelService.syncData(lists,textArea);
//        for (List<String> list : lists) {
//            for (String s : list) {
//                System.out.print(s+",");
//                textArea.appendText(s+",");
//            }
//            textArea.appendText("\n");
//            System.out.println();
//        }
    }


    public static void main(String[] args) {
        launch(args);
    }



    public void relaunch(){
        Platform.runLater(() -> {
            getStage().close();
            try {
                this.stop();
                this.init();
                this.start(new Stage());
            } catch (Exception e) {
                Logger.getLogger(FileChooserSample.class.getName()).log(
                        Level.SEVERE, null, e
                );
            }
        });
    }
}
